<?php
$lon = $_GET["lon"];
$lat = $_GET["lat"];
$id = $_GET["id"];
// Include the configuration and function files we created
require '../config.php';
// Establish a MySQL connection and select our database using values contained in config.php.
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (!empty($lon) && !empty($lat)){

  if (is_numeric($lon) && is_numeric($lat)){
    $query = "SELECT name,userstatus,glength(linestring(point(longitude,latitude),point(".$lon.",".$lat.")))*1.1*100000 as dist FROM crombtracker where id='".$id."'";
     //echo $query; 
     if ($stmt = $mysqli->prepare($query)){
      $stmt->execute();
      $stmt->bind_result($name,$status,$dist);
      echo "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<root>\n";
      while ($stmt->fetch()){
        printf("\t<crombtrack name='%s' status='%s' dist='%s'>\n", $name,$status,$dist);
        $count=$count+1;
      }
      echo "</root>\n";
      $stmt->close();
      
     }
  }
}
// cleanup old tracks from other gamers...
$delete = "delete from crombtracker where lastupdate < TIMESTAMPADD(HOUR,-1,NOW())"; 
mysqli_query($mysqli ,$delete);
$mysqli->close();

?>